# -*- coding: utf-8 -*-
# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from tipfy.ext import jinja2
import datetime
import re
import logging
from jinja2 import environmentfilter, Markup, escape
from jinja2.filters import evalcontextfilter

# Person finder instance base URL
# (i.e. 'http://japan.person-finder.appspot.com/'
PF = ''

PF_DEV_KEY = ''
PF_DEV_URL = ''
PF_DEV_SUBDOMAIN = ''
PF_DEV_DOMAIN = ''

PF_PROD_KEY = ''
PF_PROD_URL = ''
PF_PROD_SUBDOMAIN = ''
PF_PROD_DOMAIN = ''

_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

@evalcontextfilter
def nl2br(eval_ctx, value):
    logging.info(value)
    result = ''
    for p in _paragraph_re.split(escape(value)):
      result += u'<p>%s</p>' % p.unescape().replace('\n', '<br>\n')
    logging.info(result)
    if eval_ctx.autoescape:
        result = Markup(result)
    return result

config = {}

# Configurations for the 'tipfy' module.
config['tipfy'] = {
    # Enable debugger. It will be loaded only in development.
    'middleware': [
        'tipfy.ext.debugger.DebuggerMiddleware',
        'tipfy.ext.i18n.I18nMiddleware',
    ],
    'apps_installed': [
        'apps.pf_mobile',
    ],
}

def datetimeformat(value):
    dt = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
    delta = datetime.timedelta(hours=9)
    return (dt + delta).strftime('%Y/%m/%d %H:%M')

def get_jinja2_env():
    env = jinja2.create_jinja2_instance()
    env.add_extension('jinja2.ext.autoescape')
    env.add_extension('jinja2.ext.i18n')
    env.autoescape = True
    env.filters.update({
        'datetimeformat': datetimeformat
    })
    return env

config['tipfy.ext.jinja2'] = {
    'templates_compiled_target': 'compiled_templates.zip',
    'engine_factory': get_jinja2_env
}

config['tipfy.ext.i18n'] = {
    'locale': 'ja_JP',
    'timezone': 'Asia/Tokyo',
}
